﻿using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_SpecialityKindsDetailEditForm_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_SpecialityKindsDetailEditForm_List()
        {
            InitializeComponent();
            BindInfoKinds();
        }
        private void BindInfoKinds()
        {
            using (DataTable dt = service.GetSpecialityKindsTable("","","",""))
            {
                grid_ZY.DataSource = dt;

                GridView rowsSelected = gridView_ZY as GridView;
                if (rowsSelected.Rows.Count > 0)
                {
                    int[] inteurm = rowsSelected.GetSelectedRows();
                    if (inteurm != null && inteurm.Length > 0)
                    {
                        string strSpecialityKindsNo = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                        BindInfoKindsDetail(strSpecialityKindsNo);
                    }
                }
                else
                {
                    gridZY_Detail.DataSource = null;
                    gridZY_Detail_Sub.DataSource = null;
                }
            }
        }

        private void gridView_ZY_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            GridView rows = sender as GridView;
            if (rows.Rows.Count > 0)
            {
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string strSpecialityKindsNo = rows.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                    gridZY_Detail.DataSource = null;
                    gridZY_Detail_Sub.DataSource = null;
                    BindInfoKindsDetail(strSpecialityKindsNo);
                }
            }
        }
        private string GetSpecialityKindsNo()
        {
            string strSpecialityKindsNo = string.Empty;
            GridView rowsSelected = gridView_ZY as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                }
            }
            return strSpecialityKindsNo;
        }

        private string GetSpecialityKindsNo_Detail()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView_ZY_Detail as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private string GetSpecialityKindsNo_Detail_Sub()
        {
            string strSpecialityKindsNo_DetailSub = string.Empty;
            GridView rowsSelected = gridView_ZY_DetailSub as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_DetailSub = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                }
            }
            return strSpecialityKindsNo_DetailSub;
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            string strSpecialityKindsNo = string.Empty;
            GridView rowsSelected = gridView_ZY as GridView;
            int[] inteurm = rowsSelected.GetSelectedRows();
            if (inteurm != null && inteurm.Length > 0)
            {
                strSpecialityKindsNo = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                Base_SpecialityKindsDetailEditForm_Option option = new Base_SpecialityKindsDetailEditForm_Option(strSpecialityKindsNo);
                if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    BindInfoKindsDetail(strSpecialityKindsNo);
                }
            }
            else
            {
                DevHelper.ShowInfoMessageBox("请先选择专业!");
            }
        }
        private void BindInfoKindsDetail(string strSpecialityKindsNo)
        {         
            DataTable dt = service.GetSpecialityKindsDetailTable(strSpecialityKindsNo);
            gridZY_Detail.DataSource = dt;
            GridView rowsSelected = gridView_ZY_Detail as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                    BindInfoKindsDetailSub(strSpecialityKindsNo_Detail);
                }
            }
            else
            {
                gridZY_Detail_Sub.DataSource = null;
            }
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            string strSpecialityKindsNo_Detail = GetSpecialityKindsNo_Detail();
            if (strSpecialityKindsNo_Detail.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择细分项!");
            }
            else
            {
                string strSpecialityKindsNo = GetSpecialityKindsNo();
                service.DeleteSpecialityKindsDetail(strSpecialityKindsNo, strSpecialityKindsNo_Detail);
                BindInfoKindsDetail(strSpecialityKindsNo);
            }
        }

        private void gridView_ZY_Detail_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            gridZY_Detail_Sub.DataSource = null;
            GridView rows = sender as GridView;
            if (rows.Rows.Count > 0)
            {
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string strSpecialityKinds_DetailNO = rows.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                    BindInfoKindsDetailSub(strSpecialityKinds_DetailNO);
                }
            }
        }

        private void gridView_ZY_Detail_DoubleClick(object sender, EventArgs e)
        {
            GridView rows = sender as GridView;
            if (rows.Rows.Count > 0)
            {
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string SpecialityKindsNO = rows.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                    string SpecialityKindsNO_Detail = rows.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                    string SpecialityKindsNO_DetailName = rows.Rows[inteurm[0]].DataRow.ItemArray[2].ToString();
                    Base_SpecialityKindsDetailEditForm_Option option = new Base_SpecialityKindsDetailEditForm_Option(SpecialityKindsNO_Detail, SpecialityKindsNO_DetailName, SpecialityKindsNO);
                    if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        BindInfoKindsDetail(SpecialityKindsNO);
                    }
                }
            }
        }
        private void BindInfoKindsDetailSub(string strSpecialityKinds_DetailNO)
        {
            DataTable dt = service.GetSpecialityKinds_Detail_SubDataTable(strSpecialityKinds_DetailNO,"1");
            gridZY_Detail_Sub.DataSource = dt;
        }

        private void btnAddItemSub_Click(object sender, EventArgs e)
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView_ZY_Detail as GridView;
            if (rowsSelected.Rows.Count == 0)
            {
                DevHelper.ShowInfoMessageBox("请先新增细分项!");
            }
            else
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                    Base_SpecialityKindsDetailEditForm_Sub_Option option = new Base_SpecialityKindsDetailEditForm_Sub_Option(strSpecialityKindsNo_Detail);
                    if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        BindInfoKindsDetailSub(strSpecialityKindsNo_Detail);
                    }
                }
                else
                {
                    DevHelper.ShowInfoMessageBox("请先选择细分项!");
                }
            }
        }

        private void btnDeleteItemSub_Click(object sender, EventArgs e)
        {
            string strSpecialityKindsNo_Detail_Sub = GetSpecialityKindsNo_Detail_Sub();
            if (strSpecialityKindsNo_Detail_Sub.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择细分项!");
            }
            else
            {
                string strSpecialityKindsNo_Detail = GetSpecialityKindsNo_Detail();
                service.DeleteSpecialityKinds_Detail_Sub(strSpecialityKindsNo_Detail, strSpecialityKindsNo_Detail_Sub);
                BindInfoKindsDetailSub(strSpecialityKindsNo_Detail);
            }
        }

        private void gridView_ZY_DetailSub_DoubleClick(object sender, EventArgs e)
        {
            GridView rows = sender as GridView;
            if (rows.Rows.Count > 0)
            {
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string SpecialityKindsNO_Detail = rows.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                    string SpecialityKindsNO_Detail_Sub = rows.Rows[inteurm[0]].DataRow.ItemArray[1].ToString();
                    string SpecialityKindsNO_DetailName_Sub = rows.Rows[inteurm[0]].DataRow.ItemArray[2].ToString();
                    Base_SpecialityKindsDetailEditForm_Sub_Option option = new Base_SpecialityKindsDetailEditForm_Sub_Option(SpecialityKindsNO_Detail_Sub, SpecialityKindsNO_DetailName_Sub, SpecialityKindsNO_Detail);
                    if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        BindInfoKindsDetailSub(SpecialityKindsNO_Detail);
                    }
                }
            }
        }
    }
}
